package com.common.android.utils;

import java.text.NumberFormat;
/**
 * 格式转换相关工具类
 * <br>Created 2014-8-27 下午2:44:32
 * @version  
 * @author   huangyx		
 * @see 	 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public class FormatUtils {

    private static final int KB_INT = 1048576; //1024 * 1024
    private static final int MB_INT = 1048576; //1024 * 1024
    private static final int GB_INT = 1073741824; //1024 * 1024 * 1024
    private static final double KB_DOUBLE = 1024.0;
    private static final double MB_DOUBLE = 1048576.0;
    private static final double GB_DOUBLE = 1073741824.0;
    private static final double MS_DOUBLE = 1000.0;
    
    /**
     * 毫秒转成秒, 如果有余数加1
     * <br>Created 2014-4-16下午3:33:44
     * @param time 输入的毫秒
     * @return 秒
     * @author zhy
     */
    public static int  millsec2Sec(long time){      
        return Double.valueOf(Math.ceil(time / MS_DOUBLE)).intValue();
    }

    /**
     * 将double类型的文件大小转换成型String类型
     * <br>Created 2014-8-22 上午11:42:44
     * @param d 双精度数字
     * @param dot 后跟小数点位数
     * @return 返回特定格式的字符串 
     * @author       huangyx 
     */
    public static String formatNumber(double d, int dot) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumFractionDigits(dot);
        nf.setMaximumFractionDigits(dot);
        if (d > GB_INT) {
            d = d / GB_DOUBLE;
            return nf.format(d) + "GB";
        } else if (d > MB_INT) {
            d = d / MB_DOUBLE;
            return nf.format(d) + "MB";
        } else {
            d = d / KB_DOUBLE;
            return nf.format(d) + "KB";
        }
    }
    
    /**
     * 将String类型的文件大小转换成为double型
     * <br>Created 2014-8-22 上午11:30:44  
     * @param s 双精度数字
     * @return 如果字符串不为null返回double类型数值，如果字符串为null，返回0
     * @author       huangyx   
     */
    public static double parseRelugarDouble(String s) {
        double relvalue;
        int index;
        if (s == null || s.equals("")) {
            return 0;
        } else {
            if ((index = s.indexOf("K")) != -1) {
                relvalue = parseDouble(s.substring(0, index)) * KB_INT;
                return relvalue;
            } else if ((index = s.indexOf("M")) != -1) {
                relvalue = parseDouble(s.substring(0, index)) * MB_INT;
                return relvalue;
            } else if ((index = s.indexOf("G")) != -1) {
                relvalue = parseDouble(s.substring(0, index)) * GB_INT;
                return relvalue;
            } else {
                return 0;
            }
        }
    }
    
    /**
     * 将String类型字符串转换成double型
     * <br>Created 2014-8-22 上午11:40:44  
     * @param s 双精度数字
     * @return 如果字符串不为null返回double类型数值，如果字符串为null，返回0
     * @author       huangyx 
     */
    public static double parseDouble(String s) {
        double d = 0;
        if (s != null) {
            try {
                d = Double.parseDouble(s);
            } catch (NumberFormatException e) {
                try {
                    String t = s.replaceAll(",", "");
                    d = Double.parseDouble(t);
                } catch (NumberFormatException e2) {
                    d = 0;
                }
            }
        }
        return d;
    }

    
    /**
     * 将String类型数据转换为int型
     * <br>Created 2014-8-21 下午3:12:18
     * @param s 双精度数字
     * @return 如果字符串不为null返回int类型数值，如果字符串为null，返回0
     * @author       huangyx        
     */
    public static int parseInt(String s) {
        int i = 0;
        if (s != null) {
            try {
                i = Integer.parseInt(s);
            } catch (NumberFormatException e) {
                i = 0;
                double t = parseDouble(s);
                if (t != 0) {
                    i = (int) t;
                }
                // logger.error(e.getMessage());
            }
        }
        return i;
    }
    
    /**
     * 将String为类型数据转换为long型
     * 
     * <br>Created 2014-8-22 上午11:32:44  
     * @param s 双精度数字
     * @return 如果字符串不为null返回long类型数值，如果字符串为null，返回0
     * @author       huangyx    
     */
    public static long parseLong(String s) {
        long i = 0l;
        if (s != null) {
            try {
                i = Long.parseLong(s);
            } catch (NumberFormatException e) {
                i = 0l;
                double t = parseDouble(s);
                if (t != 0) {
                    i = (long) t;
                }
            }
        }
        return i;
    }
    
    /**
     * 将double类型数据转换为String类型.
     *  <br>Created 2014-8-22 上午11:12:44 
     * @param d 双精度数字
     * @param dot 后跟小数点位数
     * @return 返回特定格式的字符串
     * @author       huangyx  
     */
    public static String formatDoubleNumber(double d, int dot) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumFractionDigits(dot);
        nf.setMaximumFractionDigits(dot);
        return nf.format(d);
    }
    
}
